package com.ethink.mdm.facade.impl;

import com.ethink.mdm.api.AccountFacade;
import com.ethink.mdm.api.info.CompanyAndBankInfo;
import com.ethink.mdm.api.model.CompanyAndBankModel;
import com.ethink.mdm.api.response.ReceivingAccountQueryResponse;
import com.ethink.mdm.common.entity.CompanyAndBank;
import com.ethink.mdm.common.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName AccountFacadeImpl.java
 * @Description
 * @createTime 2022年12月19日 10:03:00
 */
@RestController
@RequestMapping("mdm-service/facade/v1/account")
public class AccountFacadeImpl implements AccountFacade {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public ReceivingAccountQueryResponse queryReceivingAccount() {
        ReceivingAccountQueryResponse response =  new ReceivingAccountQueryResponse();
        List<CompanyAndBank> companyAndBankList =  accountMapper.queryReceivingAccount();
        List<CompanyAndBankModel> infoList = new ArrayList<>();
        Map<String, List<CompanyAndBank>> group = companyAndBankList.stream().collect(Collectors.groupingBy(CompanyAndBank::getSettleCompanyCode));
        for (Map.Entry<String, List<CompanyAndBank>> stringListEntry : group.entrySet()) {
            CompanyAndBankModel info = new CompanyAndBankModel();
            List<CompanyAndBank> value = stringListEntry.getValue();
            CompanyAndBank companyAndBank = value.get(0);
            info.setSettleCompanyCode(companyAndBank.getSettleCompanyCode());
            info.setSettleCompanyName(companyAndBank.getSettleCompanyName());
            fillData(info,value);
            infoList.add(info);
        }
        response.setInfoList(infoList);
        return response;
    }

    @Override
    public CompanyAndBankInfo queryCompanyByAccount(String account) {
        return accountMapper.queryCompanyByAccount(account);
    }

    private CompanyAndBankModel fillData(CompanyAndBankModel info, List<CompanyAndBank> value) {
        List<CompanyAndBankModel.Account> item = new ArrayList<>();
        for (CompanyAndBank companyAndBank : value) {
            CompanyAndBankModel.Account account = new CompanyAndBankModel.Account();
            account.setBank(companyAndBank.getBank());
            account.setAccount(companyAndBank.getAccount());
            item.add(account);
        }
        info.setItem(item);
        return info;
    }
}

